﻿////****************************************************************************
////
//// Descriptive Name = A  data access wrapper library for IBM.Data.DB2.iSeries 
//// provider.
//// Short Name = ISeriesDataProxy
//// Function = A data access proxy for client applications. 
////
//// Copyright for IBM.Data.DB2.iSeries and other IBM libraries used belongs to 
//// IBM Corp 2008
////
////****************************************************************************
////============================================================================
//// Copyright © Shibu Raj - shibu.raj@gmail.com.  All Rights Reserved.
//// This code released under the terms of the 
//// GNU General Public License version 2 (GPLv2) 
//// (http://isereisdataproxy.codeplex.com/license)
////============================================================================
namespace ISeriesDataProxy
{
    using System;
    using System.Configuration;

    /// <summary>
    /// Factory to create instances of DB2 Database objects.
    /// </summary>
    public static class Db2DatabaseFactory
    {
        /// <summary>
        /// Default connection string configuration name to use.
        /// </summary>
        private const string DEFAULTCONFIGSOURCENAME = "default";

        /// <summary>
        /// Initilizes an instance of the Db2Database.
        /// </summary>
        /// <returns>An instance of the Db2Database</returns>
        public static Db2Database CreateDatabase()
        {
            Db2Database database;
            try
            {
                database = new Db2Database(
                    GetConnectionStringFromConfigurationSource(
                    DEFAULTCONFIGSOURCENAME));
            }
            catch (Exception)
            {
                throw;
            }

            return database;
        }

        /// <summary>
        /// Initializes and instance of the Db2Instance with the specified
        /// connection string.
        /// </summary>
        /// <param name="connectionName">
        /// Name of the connection string configuration key to use.
        /// </param>
        /// <returns>An instance of Db2Database.</returns>
        public static Db2Database CreateDatabase(string connectionName)
        {
            Db2Database database;
            try
            {
                database = 
                    new Db2Database(
                        GetConnectionStringFromConfigurationSource(
                        connectionName));
            }
            catch (Exception)
            {
                throw;
            }

            return database;
        }

        #region Private Methods

        /// <summary>
        /// Private helper method to fetch the connection string
        /// </summary>
        /// <param name="name"> Name of the connection string to use.</param>
        /// <returns>
        /// Returns the connection string from the configuration file.
        /// </returns>
        private static string GetConnectionStringFromConfigurationSource(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException("ConnectionString name canoot be empty.", "ConnectionString");
            }

            string connectionString = 
                ConfigurationManager.ConnectionStrings[name].ConnectionString;
            if (String.IsNullOrEmpty(connectionString))
            {
                throw new ArgumentException("Connection String cannot be empty.", "ConnectionString");
            }

            return connectionString;
        }

        #endregion
    }
}